#include "Tribe.h"
#include "Choice.h"
#include "Utilities.h"

using namespace Hunger;

namespace Hunger
{
	Tribe::Tribe(int numPeople) : foodstore(100), morale(100), empathy(100),
					 weather(0), disaster(0), pestilence(0), dailyKills(0)
	{
		for(int person = 0; person < numPeople; person++)
		{
			Human * human;
			human = new Human();

			people.push_back(human);
		}
		for(int person = 0; person < people.size(); person++)
		{
			people.at(person)->CreateAffinities(people);
		}
	}

	Tribe::~Tribe()
	{
		for(std::vector<Human *>::iterator tribeItor = people.begin(); tribeItor != people.end(); tribeItor++)
		{
			delete *tribeItor;
			*tribeItor = 0;
		}
	}
	void Tribe::UpdateSelf()
	{
		this->FigureFoodSupply();
		this->FigureMorale();
		this->FigureEmpathy();

		this->AffectPeople();

		this->Hunt();
		this->PreserveMeat();
	}

	void Tribe::DisplaySelf()
	{
		std::cout << "\nMain Tribe:" << std::endl;

		std::string food_supply;
		if(foodstore <= FoodSupply::Famine)
		{
			food_supply = "We have nothing! God has forsaken us!";
		}
		else if(foodstore <= FoodSupply::Poverty)
		{
			food_supply = "Food is becoming scarce. What will we do?";
		}
		else if(foodstore <= FoodSupply::Adequate)
		{
			food_supply = "We have what we need, for now.";
		}
		else if(foodstore <= FoodSupply::Plentiful)
		{
			food_supply = "We have so much to be thankful for!";
		}
		else if(foodstore <= FoodSupply::Cornucopia)
		{
			food_supply = "We are overflowing with abundance! Glory be to God!";
		}
		
		std::cout << "Food Supply - " << food_supply << "\n";

		std::string currentMorale;
		if(morale <= Morale::Suffering)
		{
			currentMorale = "Who can ease our suffering!";
		}
		else if(morale <= Morale::Depressed)
		{
			currentMorale = "The people are quite depressed.";
		}
		else if(morale <= Morale::Content)
		{
			currentMorale = "We live day by day, God willing.";
		}
		else if(morale <= Morale::Happy)
		{
			currentMorale = "The people are quite happy!";
		}
		else if(morale <= Morale::Joyful)
		{
			currentMorale = "This is cause for celebration!";
		}	

		std::cout << "Morale - " << currentMorale << "\n";

		std::string currentEmpathy;
		if(empathy <= Empathy::Malevolent)
		{
			currentEmpathy = "Others must suffer so that we do not!";
		}
		else if(empathy <= Empathy::Hateful)
		{
			currentEmpathy = "Those others take all that should belong to us!";
		}
		else if(empathy <= Empathy::Lukewarm)
		{
			currentEmpathy = "We don't think much of others, honestly.";
		}
		else if(empathy <= Empathy::Benevolent)
		{
			currentEmpathy = "All are one under the sun!";
		}
		else if(empathy <= Empathy::Kind)
		{
			currentEmpathy = "We wish others good will and charity!";
		}
		std::cout << "Empathy - " << currentEmpathy << "\n";
	}

	void Tribe::AffectPeople()
	{
		for(int person = 0; person < people.size(); person++)
		{
			float change;

			//how the morale of the tribe affects the individual's morale
			int individualMorale = people.at(person)->GetMorale();
			if(morale < individualMorale)
			{
				change = ((float)morale / (float)individualMorale) * 10.0f;
				individualMorale -= (int)(change);
			}
			else
			{
				change = ((float)individualMorale / (float)morale) * 10.0f;
				individualMorale += (int)(change);
			}

			//how the empathy of the tribe affects the individual's empathy
			int individualEmpathy = people.at(person)->GetEmpathy();
			if(empathy < individualEmpathy)
			{
				change = ((float)empathy / (float)individualEmpathy) * 10.0f;
				individualEmpathy -= (int)(change);
			}
			else
			{
				change = ((float)individualEmpathy / (float)empathy) * 10.0f;
				individualEmpathy += (int)(change);
			}

			int newMorale = individualMorale;
			int newEmpathy = individualEmpathy;

			people.at(person)->SetMorale(newMorale);
			people.at(person)->SetEmpathy(newEmpathy);
		}
	}

	void Tribe::FigureFoodSupply()
	{
		for(int person = 0; person < people.size(); person++)
		{
			foodstore--;
		}
		if(foodstore < 0)
		{
			foodstore = 0;
		}
	}

	void Tribe::FigureMorale()
	{
		//morale is calculated as the average of all individual morales
		int moraleSum = 0;
		for(int person = 0; person < people.size(); person++)
		{
			int m = people.at(person)->GetMorale();
			moraleSum += m;
		}
		float moralePercentage = ((float)moraleSum / (float)people.size());

		morale = (int)moralePercentage;
	}

	void Tribe::FigureEmpathy()
	{
		//empathy is calculated as the average of all individual empathies
		int empathySum = 0;
		for(int person = 0; person < people.size(); person++)
		{
			int e = people.at(person)->GetEmpathy();
			empathySum += e;
		}
		float empathyPercentage = ((float)empathySum / (float)people.size());

		empathy = (int)empathyPercentage;
	}

	void Tribe::HoldCouncil()
	{
	}

	void Tribe::Hunt()
	{
		dailyKills = Utilities::Random(0, 20);
		std::cout << "\nHunters go hunting...bring back " << dailyKills << " kills" << std::endl;
	}

	void Tribe::PreserveMeat()
	{
		int min = static_cast<int>(dailyKills * .50);
		int preserved = Utilities::Random(min , dailyKills) * 2;
		foodstore += preserved;
		std::cout << "Preservers are able to preserve " << preserved << " food portions today...\n" << std::endl;
	}
}